/* 
This do file counts the number of solar installations in each U.S. zip code.

NOTES: All directories and paths should be set in the main "Create_Number_Systems_Files.do" file.

*/

clear all

use `"${data_path}/openpv_all_2018-07-22.dta"'

gen num_systems=1

*drop utility installations
drop if install_type=="utility" | install_type=="Utility"

drop if missing(zipcode)

drop year
gen int year = real(substr(date_installed,-4,.))

*drop 2016-2018 data; we'll append 2016-2017 data from TTSXI below
drop if year>2015

*drop all unnecessary variables in order to append TTSXI data
drop  date_installed incentive_prog_names type size_kw appraised install_type installer cost_per_watt cost lbnl_tts_version_year lbnl_tts city utility_clean tech_1 model1_clean county annual_pv_prod annual_insolation rebate sales_tax_cost tilt1 tracking_type azimuth1 manuf2_clean manuf3_clean manuf1_clean inv_man_clean reported_annual_energy_prod incentivetype year_app_implied npv_fit_real application_implied npv_pbi_real other_incentive appraised_cluster inflation other_incentive_real zip_available cust_city pbi pbi_real pbi_length application fit_length fit_rate fit_payment _3rdparty_implied utility install_price_real_w install_price installer_clean manuf1_ inverter_reported rebate_real model1 _3rdparty inv_model_reported microinv_solarhub bipv_3 bipv_2 bipv_1 sales_tax_rate sales_tax_cost_real bipv_all thinfilm_all china sys_sizeac pbi_rate new_constr effic_1 cust_county tracking inv_model_clean mod_cost_real inv_cost_real bos_powerclerk_real permitting_real rdparty

*append TTSXI data for only 2016-2017
append using `"${data_intermediate}/NumberSystems_TTSXI_2016-2017.dta"'

recast str state

collapse (firstnm) state (sum) num_systems, by(zipcode)

save `"${data_intermediate}/NumberSystems"', replace
